<?php

if(isset($_GET['numPageArt']))
{
	$from= (NB_ARTICLES_PER_PAGE*($_GET['numPageArt']-1));
}
else
{
	$from=0;
}

if(isset($_GET['categ']))
{
	$cat = Category::read($_GET['categ']);
	$catName = $cat->getNameCat();
	
	//Main category with subcategories
	if(0 == $cat->getIdParentCat())
	{
		
		//$query = "WHERE ART_IdCategorie IN(SELECT CAT_Id FROM categories WHERE CAT_IdParent = ".$_GET['categ'].")";
		$totalArticles = count(Article::listing("WHERE ART_IdCategorie IN(SELECT CAT_Id FROM categories WHERE CAT_IdParent = ".$_GET['categ']));
		$articlesList = Article::listing("WHERE ART_IdCategorie IN(SELECT CAT_Id FROM categories WHERE CAT_IdParent = ".$_GET['categ'].") ORDER BY ART_Date DESC LIMIT ".$from.",".NB_ARTICLES_PER_PAGE);		
	}
	else
	{
		$totalArticles = count(Article::listing("WHERE ART_IdStatut = 2 AND  ART_IdCategorie = ".$cat->getIdCat()));
		$articlesList = Article::listing("WHERE ART_IdStatut = 2 AND  ART_IdCategorie = ".$cat->getIdCat() ." ORDER BY ART_Date DESC LIMIT ".$from.",".NB_ARTICLES_PER_PAGE);
	}	
	
}
else
{
	$totalArticles = count(Article::listing("WHERE ART_IdStatut = 2"));
	$articlesList = Article::listing("WHERE ART_IdStatut = 2 ORDER BY ART_Date DESC LIMIT ".$from.",".NB_ARTICLES_PER_PAGE);
	$catName = "Home";
}
 
//Pagination
$nbPages=ceil($totalArticles/NB_ARTICLES_PER_PAGE);
$pagination = false;

//echo count($articlesList)." et ".NB_ARTICLES_PER_PAGE;
if(count($articlesList)>NB_ARTICLES_PER_PAGE)
{
	$pagination = true;
	echo "DEBUG";
	//include ("partials/list_articles.php");
}	

if(null !== $articlesList[0])
{
	include ("partials/list_articles.php");
}
else
{
	echo $catName."<br/>";
	echo "Cette cat&eacute;gorie ne contient pas d'article.";
}
		
			


?>